package com.yxjme.egcviewlibrary.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;


import com.yxjme.egcviewlibrary.R;

import java.util.ArrayList;


public class ECGAllDataView extends View {

    /**本页面宽，高*/
    private int width,height;
    private ArrayList<String> data_source;
    /**总的数据个数*/
    private int data_num;
    /**下方矩形区域心电图的中心Y值*/
    private float rectY_center;
    /**两点间横坐标间距*/
    private float gap_x = 10 ;
    /**默认支持绘制网格*/
    private boolean isSupportGrid = true;
    /**默认支持绘制竖线*/
    private boolean isSupportGrid_ver = true;
    /**默认支持绘制横线*/
    private boolean isSupportGrid_hor = true ;
    /**是否需要支持实线*/
    private boolean isSupportShiXian = true;
    /**虚线点的宽和点点间隔*/
    private int lineWidth = 1;
    private int lineStorkWidth = 2;
    private int lineSpeed = 5;
    /**设置虚线的颜色*/
    private int lineColor = Color.RED;
    /*横向虚线条数*/
    private int grid_hori;
    /*竖向虚线条数*/
    private int grid_ver;
    /*线与线之间的间隔距离*/
    private float gap_grid = 30.0f;


    public ECGAllDataView(Context context, AttributeSet attrs){
        super(context,attrs);
        //背景色 透明
        this.setBackgroundColor(getResources().getColor(R.color.trans));
    }

    public ECGAllDataView(Context context){
        super(context);
        //背景色 透明
        this.setBackgroundColor(getResources().getColor(R.color.trans));
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpecWidth(widthMeasureSpec),MeasureSpecHeight(heightMeasureSpec));
    }

    /**
     * 测量view 高度
     * @param heightMeasureSpec
     * @return
     */
    private int MeasureSpecHeight(int heightMeasureSpec) {
        final int minimumHeight = getSuggestedMinimumHeight();
        int h = 0 ;
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY){
            /*固定值*/
            h =  height ;
        }else {
            if (heightMode ==MeasureSpec.AT_MOST){
                h =  getPaddingBottom() + getPaddingTop() + 200;
            }else {
                h = Math.max(minimumHeight, height);
            }
        }
        return h;
    }

    /**
     * 测量view的宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int MeasureSpecWidth(int widthMeasureSpec) {
        final int minimumWeight =getSuggestedMinimumWidth ();
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int w = 0 ;
        if (widthMode == MeasureSpec.EXACTLY){
            /*固定值*/
            w =  width ;
        }else {
            if (widthMode ==MeasureSpec.AT_MOST){
                w = (int) (gap_x * (data_source==null?0:data_source.size()-1));
            }else {
                w = Math.max(minimumWeight, width);
            }
        }
        return w ;
    }



    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (changed){
            width = getWidth();
            height = getHeight();

            grid_hori = (int) (height / gap_grid);
            grid_ver = (int) (width / gap_grid);

            data_num = data_source==null?0:data_source.size();

            rectY_center = (float)height/2;
        }
        super.onLayout(changed, left, top, right, bottom);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        DrawGrid(canvas);
        DrawAllData(canvas);
        mean();
    }





    /**
     * @param lineColor
     */
    public ECGAllDataView setLineColor(int lineColor) {
        this.lineColor = lineColor;
        return ECGAllDataView.this ;
    }


    public ECGAllDataView setLineStorkWidth(int lineStorkWidth) {
        this.lineStorkWidth = lineStorkWidth;
        return ECGAllDataView.this ;
    }


    public ECGAllDataView setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
        return ECGAllDataView.this;
    }

    public ECGAllDataView setLineSpeed(int lineSpeed) {
        this.lineSpeed = lineSpeed;
        return ECGAllDataView.this ;
    }

    /**
     * 是否支持每个五条之后绘制实线
     * @param b
     * @return
     */
    public ECGAllDataView isSupportShiXian(boolean b) {
        this.isSupportShiXian = b ;
        return ECGAllDataView.this;
    }

    /**
     * 是否支持 绘制网格
     *
     * @param b
     * @return
     */
    public ECGAllDataView isSupportGrid(boolean b){
        this.isSupportGrid=b ;
        return ECGAllDataView.this ;
    }

    /**
     * 是否支持绘制竖线
     *
     * @param b
     * @return
     */
    public ECGAllDataView isSupportGrid_ver(boolean b) {
        this.isSupportGrid_ver=b;
        return ECGAllDataView.this;
    }


    /**
     * 是否支持绘制横线
     * @param b
     * @return
     */
    public ECGAllDataView isSupportGrid_hor(boolean b) {
        this.isSupportGrid_hor = b ;
        return ECGAllDataView.this;
    }





    /**
     * 画背景网格
     */
    private void DrawGrid(Canvas canvas){

        /*是否支持绘制网格*/
        if (isSupportGrid){


            if (isSupportGrid_hor){
                //横线
                for (int i = 1 ; i < grid_hori + 2 ; i ++){
                    Paint paint = new Paint();
                    paint.setStyle(Paint.Style.STROKE);
                    paint.setColor(lineColor); //<color name="data_pr">#0a7b14</color>
                    paint.setStrokeWidth(lineStorkWidth);
                    Path path = new Path();
                    path.moveTo(0, gap_grid * (i-1) + (height-grid_hori*gap_grid)/2);
                    path.lineTo(width,gap_grid * (i-1) + (height-grid_hori*gap_grid)/2);


                    if (isSupportShiXian){
                        if ( i % 5 != 0 ){//每第五条，为实线   其余为虚线 ，以下为画虚线方法
                            /*参数解释  float[] 第一个参数表示虚线长度 ，第二个参数表示虚线点间隔距离*/
                            PathEffect effect = new DashPathEffect(new float[]{lineWidth,lineSpeed},1);
                            paint.setPathEffect(effect);
                        }
                    }else {
                        PathEffect effect = new DashPathEffect(new float[]{lineWidth,lineSpeed},1);
                        paint.setPathEffect(effect);
                    }


                    canvas.drawPath(path,paint);
                }
            }



            if (isSupportGrid_ver){
                //竖线
                for (int i = 1 ; i < grid_ver + 2 ; i ++){
                    Paint paint = new Paint();
                    paint.setStyle(Paint.Style.STROKE);
                    paint.setColor(lineColor);
                    paint.setStrokeWidth(lineStorkWidth);
                    Path path = new Path();
                    path.moveTo(gap_grid * (i-1) + (width-grid_ver*gap_grid)/2, 0);
                    path.lineTo(gap_grid * (i-1) + (width-grid_ver*gap_grid)/2,height);

                    if (isSupportShiXian){
                        if (i % 5 != 0 ){
                            PathEffect effect = new DashPathEffect(new float[]{lineWidth,lineSpeed},1);
                            paint.setPathEffect(effect);
                        }
                    }else {
                        PathEffect effect = new DashPathEffect(new float[]{lineWidth,lineSpeed},1);
                        paint.setPathEffect(effect);
                    }
                    canvas.drawPath(path,paint);
                }
            }
        }
    }



    /**
     * 画下方矩形区域的心电图
     */
    private void DrawAllData(Canvas canvas){
        this.canvas = canvas ;
        if (data_source==null||data_source.size()==0)return;
        Paint paint = new Paint();
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(getResources().getColor(R.color.red));
        paint.setStrokeWidth(3.0f);
        CornerPathEffect cornerPathEffect = new CornerPathEffect(200);
        paint.setPathEffect(cornerPathEffect);

        Path path = new Path();

        path.moveTo(0, getRectY_coordinate(data_source.get(0)));

        for (int i = 1 ; i < this.data_source.size() ; i ++){
            path.lineTo(gap_x * i , getRectY_coordinate(data_source.get(i)));
        }

        canvas.drawPath(path,paint);
    }




    /**
     * 将数值转换为y坐标，下方矩形 显示心电图的区域
     */
    private float getRectY_coordinate(String data){
        int y_int = Integer.parseInt(data);
        y_int = (y_int - 2048) *(-1);
        float y_coor = 0.0f;

        y_coor = y_int * 1 / 2 + rectY_center;
//        Log.v("json","<rectY_center> " + rectY_center + " < y_coor >" + y_coor +"  height:" + height +" rect_hight " + rect_high);
        return y_coor;
    }


    /**
     * 暴露接口，设置数据源
     */
    public void setData(ArrayList<String> data){
        this.data_source = data;
        invalidate();
    }



    public void mean(){
        if (data_source!=null){
            ViewGroup.LayoutParams p = getLayoutParams();
            p.width = (int) (gap_x * data_source.size());
            setLayoutParams(p);
        }
    }



    private Canvas canvas ;


    /*重置*/
    public void clear() {
        if (canvas !=null){
            Paint paint = new Paint();
            paint.setColor(Color.TRANSPARENT);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            canvas.drawPaint(paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
            data_source = null;
            canvas = null ;
            invalidate();
        }
    }
}
